package capricornus.res
{
	import capricornus.components.sprite.AbstractComponent;
	import capricornus.components.utils.FilterUtils;
	import capricornus.components.utils.MovieClipUtils;
	import capricornus.events.ResLoaderEvent;
	import capricornus.utils.MathUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.geom.Matrix;
	
	[Event(name="complete", type="capricornus.events.ResLoaderEvent")]
	public class Image extends AbstractComponent
	{
		private var _fitType:int;
		
		private var _lastUrl:String = "";
		private var _url:String = "";
		private var _loader:Loader;
		
		private var _w:int = -1;
		private var _h:int = -1;
		
		private var _defaultBitmapData:BitmapData = null;
		private var _imageDecodingPolicy:String = "";
		
		public function Image( fitType:int=ImageFitType.EXACT_FIT )
		{
			super();
			_fitType = fitType;
			init();
		}
		
		private function init():void
		{
		}
		
		public function destroy():void
		{
			if( _url )
			{
				releaseLastSource();
			}
			if( hasEventListener( Event.ADDED_TO_STAGE ) )
			{
				removeEventListener( Event.ADDED_TO_STAGE, onAddToStage );
			}
		}
		
		/**
		 * 设置图片未加载成功之前的默认图片 
		 * @param bitmapData
		 * 
		 */		
		public function set defaultBitmapData( bitmapData:BitmapData ):void
		{
			_defaultBitmapData = bitmapData;
		}
		
		/**
		 * 图片异步解码方式 4.6sdk才有用
		 * @param value
		 * 
		 */		
		public function set imageDecodingPolicy( value:String ):void
		{
//			_imageDecodingPolicy = value;
		}
		
		override public function setSize( w:int, h:int ):void
		{
			super.setSize( w, h );
			
			_w = w;
			_h = h;
			
			resize();
		}
		
		private function releaseLastSource():void
		{
			if( _container.numChildren > 0 )
			{
				var bm:Bitmap = _container.getChildAt(0) as Bitmap;
				if( bm.bitmapData )
				{
					bm.bitmapData = null;
				}
				_container.removeChildAt(0);
			}
			if( ImageManager.instance.hasEventListener( _url ) )
			{
				ImageManager.instance.removeEventListener( _url, onLoaded );
			}
			ImageManager.instance.releaseImage( _url );
			_url = "";
			_lastUrl = "";
		}
		
		public function set source( value:String ):void
		{
			if( _url==value ) return;
			
			_lastUrl = _url;
			if( _url != "" && _url != null )
			{
				releaseLastSource();
			}
			_url = value;
			
			if( _url != "" && _url != null )
			{
				if( stage != null )
				{
					load();
				}
				else if( !hasEventListener( Event.ADDED_TO_STAGE ) )
				{
					addEventListener( Event.ADDED_TO_STAGE, onAddToStage, false, 0, true );
				}
			}
		}
		
		private function onAddToStage( e:Event ):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, onAddToStage );
			load();
		}
		
		private function load():void
		{
			var bm:Bitmap = ImageManager.instance.getImage( _url );
			bm.smoothing = true;
			_container.addChildAt( bm, 0 );
			if( bm.bitmapData )
			{
				resize();
				MovieClipUtils.callLater( callLater );
			}
			else
			{
				if( _defaultBitmapData != null )
				{
					bm.bitmapData = _defaultBitmapData;
					resize();
				}
				ImageManager.instance.addEventListener( _url, onLoaded, false, 0, true );
			}
		}
		
		override public function set overSelected( value:Boolean ):void
		{
			if( false==_selected )
			{
				_overSelected = value;
				if( value )
				{
					this.filters = FilterUtils.getShineFilter();
				}
				else
				{
					this.filters = null;
				}
			}
		}
		
		override public function set selected(value:Boolean):void
		{
			super.selected = value;
			if( value )
			{
				_container.filters = FilterUtils.getSelectedFilter()
			}
			else
			{
				_container.filters = null;
			}
		}
		
		public function get source():String
		{
			return _url;
		}
		
		private function onLoaded( e:Event ):void
		{
			ImageManager.instance.removeEventListener( _url, onLoaded );
			callLater();
			resize();
		}
		
		private function callLater():void
		{
			dispatchEvent( new ResLoaderEvent( ResLoaderEvent.COMPLETE ) );
		}
		
		private function resize():void
		{
			_container.transform.matrix = new Matrix();
			var b:Boolean =  _container.width > 0 || _container.height > 0;
			if( -1 != _w && b && !_isAutoSize )
			{
				var bm:Bitmap = _container.getChildAt(0) as Bitmap;
				if( bm != null )
				{
					bm.x = bm.y = 0;
					bm.smoothing = true;
					if( _fitType==ImageFitType.CENTER )
					{
						var arr:Array = MathUtils.scaleFit( bm.bitmapData.width, bm.bitmapData.height, _w, _h );
						bm.width = arr[0];
						bm.height = arr[1];
						bm.x = int((_w - bm.width)/2);
						bm.y = int((_h - bm.height)/2);
					}
					else if( _fitType==ImageFitType.EXACT_FIT )
					{
						bm.width = _w;
						bm.height = _h;
					}
				}
			}
		}
	}
}